//
// phpMakeThumbnailFile(origFile, newFile, newWidth, newHeight)
//
// Create a new image file from an exsting file with the specified dimensions.
//
// Returns:
//				0	Success
//			  -1	Image is not a GIF, JPG, or PNG
//			  -2	Couldn't copy the image
Let (
	[
		$origFile_PARAM = origFile;
		$newFile_PARAM = newFile;
		$newWidth_PARAM = newWidth;
		$newHeight_PARAM = newHeight;

		phpCode = "error_reporting(0); //(E_ALL & ~E_NOTICE);¶
		¶
		echo MakeThumbnailFile(fm_evaluate('$origFile_PARAM'), fm_evaluate('$newFile_PARAM'), fm_evaluate('$newWidth_PARAM'), fm_evaluate('$newHeight_PARAM'));¶
		¶
		¶
		function MakeThumbnailFile($origFile, $newFile, $newWidth, $newHeight)¶
		{¶
			$imageInfo = getimagesize($origFile);¶
			$srcImg = '';¶
			if ($imageInfo[2] == IMG_JPG) $srcImg = @imagecreatefromjpeg($origFile);¶
			if ($imageInfo[2] == IMG_PNG) $srcImg = @imagecreatefrompng($origFile);¶
			if ($imageInfo[2] == IMG_GIF) $srcImg = @imagecreatefromgif($origFile);¶
			if ($srcImg == '')¶
				return -1;¶
			else¶
			{¶
				¶
				$oldWidth = imageSX($srcImg);¶
				$oldHeight = imageSY($srcImg);¶
				¶
				if (($oldWidth == $newWidth) && ($oldHeight == $newHeight))¶
				{¶
					$theWidth = $newWidth;¶
					$theHeight = $newHeight;¶
				}¶
				else if ($oldWidth > $oldHeight)¶
				{¶
					$theWidth = $newWidth;¶
					$theHeight = $oldHeight * ($newWidth / $oldWidth);¶
				}¶
				else if ($oldWidth < $oldHeight)¶
				{¶
					$theWidth = $oldWidth * ($newWidth / $oldHeight);¶
					$theHeight = $newWidth;¶
				}¶
				else if ($oldWidth == $oldHeight)¶
				{¶
					$theWidth = $newWidth;¶
					$theHeight = $newHeight;¶
				}¶
				¶
				$dstImg = ImageCreateTrueColor($theWidth, $theHeight);¶
				$didCopy = imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $theWidth, $theHeight, $oldWidth, $oldHeight);¶
				¶
				if ($didCopy)¶
				{¶
					if ($imageInfo[2] == IMG_JPG) $didCopy = imagejpeg($dstImg, $newFile, 100);¶
					if ($imageInfo[2] == IMG_PNG) $didCopy = imagepng($dstImg, $newFile);¶
					if ($imageInfo[2] == IMG_GIF) $didCopy = imagegif($dstImg, $newFile);¶
				}
				¶
				imagedestroy($dstImg);¶
				imagedestroy($srcImg);¶
				¶
				return $didCopy ? 0 : -2;¶
			}¶
		}¶
	  "
	] ;
	PHP_Execute(phpCode)
)